<template>
	<view class="content">
		<view class="title">
			<view class="title_text">您的问题和意见</view>
		</view>
		<view class="group">
			<view class="area">
				<textarea v-model="content" placeholder-style="color:#999999;font-size:34upx;line-height:41upx;" placeholder="请输入遇到的问题或者建议" />
				</view>
			<view class="img_title">添加图片(选填)</view>
			<view class="addimg">
				<view class="img_file">
					<block v-for="(image,index) in imageList" :key="index">
						<view  :class="[(index+1)%4==0?'img_content_mg':'img_content']">
							<view class="del"><image @click="del_img(index)" src="../../../static/image/del.png" mode=""></image></view>
							<image class="up_img" :src="image" :data-src="image" @tap="previewImage"></image>
						</view>
					</block>
					<view class="img_add_content" @tap="chooseImage">
						<i class="iconfont icontianjiatupian camera-css"></i>
					</view>
				</view>
			</view>
			
			
		</view>
		<view class="title">
			<view class="title_text">您的联系方式</view>
		</view>
		<view class="input_em">
			<input v-model="email" class="uni-input" focus placeholder="请输入您的联系电话或邮箱" />
		</view>
		<view @click="commit" class="btn">提交</view>
	</view>
	
</template>

<script>
	var sourceType = [
		['camera'],
		['album'],
		['camera', 'album']
	]
	var sizeType = [
		['compressed'],
		['original'],
		['compressed', 'original']
	]
	export default {
		data() {
			return {
				content:'',
				email:'',
				is_btn:true,
				name: '',
				imageList: [],
				sourceTypeIndex: 2,
				sourceType: ['拍照', '相册', '拍照或相册'],
				sizeTypeIndex: 2,
				sizeType: ['压缩', '原图', '压缩或原图'],
				countIndex: 8,
				count: [1, 2, 3, 4, 5, 6, 7, 8, 9],
				sign:''
			}
		},
		onLoad() {
			this.$zapi.apiPost('user/App/getsign').then(res=>{
						if(res.data.code==200){
							this.sign=res.data.data.sign;
						}
					});
		},
		methods: {
			chooseImage: async function() {
				uni.chooseImage({
					sourceType: sourceType[this.sourceTypeIndex],
					sizeType: sizeType[this.sizeTypeIndex],
					count: this.imageList.length + this.count[this.countIndex] > 9 ? 9 - this.imageList.length : this.count[this.countIndex],
					success: (res) => {
						this.imageList = this.imageList.concat(res.tempFilePaths);
					}
				})
			},
			previewImage: function(e) {
				var current = e.target.dataset.src
				uni.previewImage({
					current: current,
					urls: this.imageList
				})
			},
			commit: function() {
				if(this.is_btn){
					this.is_btn=false;
					if (this.content =='') {
						uni.showToast({
							title: '内容不能为空',
							icon: 'none',
							duration: 1500
						});
					}else{
						var _this = this;
						var data = {
							proposal_text: _this.content,
							contact: _this.email,
							type: 3,
							sign: _this.sign
						}
						var imgarr = [];
						if(_this.imageList.length > 0) {
							for (let i = 0; i < _this.imageList.length; i++) {
								var jsonobj = {};
								jsonobj.name = "file[" + i + "]",
								jsonobj.uri = _this.imageList[i];
								imgarr.push(jsonobj);
							}
							data['file[]'] = imgarr;
							_this.$gapi.apifile('user/Proposal/addProposal',data).then(res=>{
								if(JSON.parse(res.data).code == 200) {
									uni.showToast({
										title: '反馈成功',
										duration: 1500
									});
									setTimeout(function(){
										uni.redirectTo({
											url: 'feedback'
										});
									},1500);
								} else {
									_this.is_btn = true;
									uni.showToast({
										title: JSON.parse(res.data).msg,
										icon: 'none',
										duration: 1500
									})
								}
							})
						} else {
							_this.$gapi.apiPost('user/Proposal/addProposal',data).then(res=>{
								if(res.data.code == 200) {
									uni.showToast({
										title: '反馈成功',
										duration: 1500
									});
									setTimeout(function(){
										uni.navigateBack({
											delta: 2
										})
									},1500);
								}
							})
						}
					}
				}
			},
			del_img:function(index){
				this.imageList.splice(index,1);
			}
		}
	}
</script>

<style>
	*{
		margin: 0;
		padding: 0;
	}
	.content{
		width: 100%;
		height: 100%;
		position: absolute;
		background:rgba(243,243,243,1);
	}
	.title{
		display: flex;
		align-items: center;
		padding-left: 4%;
		width: 96%;
		height: 80upx;
		line-height: 80upx;
	}
	.title_text{
		height: 30upx;
		font-size: 34upx;
		line-height: 30upx;
		padding-left: 12upx;
		color:#333333;
		border-left:4upx solid rgba(0,213,162,1) ;
	}
	.group{
		width: 100%;
		min-height:522upx;
		background:rgba(255,255,255,1);
	}
	.area{
		width: 92%;
		height: 272upx;
		margin: auto;
		border-bottom:1px solid rgba(229,229,229,1);
	}
	.area textarea{
		line-height:41upx;
		padding-top: 22upx;
		color: #999999;
		font-size: 28upx;
		width: 100%;
		height: 100%;
	}
	.img_title{
		width: 92%;
		margin:8upx auto;
		height: 60upx;
		line-height: 60upx;
		color: #333333;
		font-size: 34upx;
	
	}
	.input_em{
		width: 98%;
		height: 72upx;
		padding-left: 2%;
	}
	.btn{
		width: 92%;
		height: 80upx;
		line-height: 80upx;
		text-align: center;
		margin: 56upx auto;
		font-size: 34upx;
		color: #FFFFFF;
		background:rgba(255,153,0,1);
	}
	/**
	 * 添加图片
	 */
	.addimg{
		width: 92%;
		margin: auto;
	
		
	}
	.img_file{
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
	}
	.img_content{
		width: 20%;
		margin-bottom: 20upx;
		height: 140upx;
		margin-right: 6%;
	}
	.img_content_mg{
		
		width: 20%;
		margin-bottom: 20upx;
		height: 140upx;
		margin-left: 0;
	}
	.img_content:first-child{
		margin-left: 0;
	}
	.img_add_content{
		border:1px solid rgba(153,153,153,0.6);
		width: 140upx;
		height: 140upx;
		margin-bottom: 20upx;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.del {
		display: flex;
		justify-content: flex-end;
		position: relative;
	}
	.del image{
		width: 32upx;
		height: 32upx;
		position: absolute;
		right: -16upx;
		top: -16upx;
		z-index: 1;
	}
	.up_img{
		width: 100%;
		height: 100%;
	}
	
	/**
	 * 
	 */
	.photo-bg {
		width: 100%;
		height: auto;
		background: #FFFFFF;
	}
		.uploader__files {
		padding-bottom: 20upx;
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
	}
	
	.uploader__file {
		width: 30%;
		margin-left: 2.5%;
		margin-top: 20upx;
	
	}
	
	
	.photo1 {
		background: #f3f3f3;
		width: 30%;
		margin-left: 2.5%;
		margin-top: 20upx;
		background-size: 100% 100%;
	}
	.photo1_bg {
		background: #f3f3f3;
		text-align: center;
	}
	.add {
		font-size: 30upx;
		color: #999999;
		margin-top: -30upx;
	}
	.camera-css {
		font-size: 60upx;
		color:rgba(153,153,153,0.6);
	}
</style>
